//
//  WBVisitorView.swift
//  weibo
//
//  Created by yanghong on 15/12/6.
//  Copyright © 2015年 杨鸿. All rights reserved.
//

import UIKit

class WBVisitorView: UIView {
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI() {
        
        backgroundColor = UIColor(white: 237 / 255.0, alpha: 1)
        
        //添加子控件
        addSubview(circleView)
        addSubview(maskIconView)
        addSubview(iconView)
        addSubview(textLabel)
        addSubview(registerButton)
        addSubview(loginBuuton)
        
        //给控件添加约束
        
        //iconView
        iconView.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0))
        //circleView
        circleView.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: circleView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: circleView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0))
        //textLabel
        // circleView 中心
        textLabel.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: textLabel, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: circleView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: textLabel, attribute: .Width, relatedBy: .Equal, toItem: nil , attribute: .NotAnAttribute, multiplier: 1, constant: 224))
        addConstraint(NSLayoutConstraint(item: textLabel, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: circleView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
        //registerButton
        registerButton.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: textLabel, attribute: NSLayoutAttribute.Leading, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 100))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 35))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: textLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
        //loginButton
        loginBuuton.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: loginBuuton, attribute: NSLayoutAttribute.Trailing, relatedBy: NSLayoutRelation.Equal, toItem: textLabel, attribute: NSLayoutAttribute.Trailing, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: loginBuuton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 100))
        addConstraint(NSLayoutConstraint(item: loginBuuton, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 35))
        addConstraint(NSLayoutConstraint(item: loginBuuton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: registerButton, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: 0))
        
        //MARK: - VFL
        //VFL 来约束maskIconView
        /**
        *  在使用VFL添加约束的时候,需要先把他添加到父控件中,不然会报错!
        */
        maskIconView.translatesAutoresizingMaskIntoConstraints = false
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[maskIconView]-0-|", options: [], metrics: nil , views: ["maskIconView": maskIconView]))
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[maskIconView]-(offset)-[registerButton]", options: [], metrics: ["offset":-35], views: ["maskIconView": maskIconView,"registerButton":registerButton]))
        
    }
    //MARK: - 设置访客视图上的 图片 和 文字
    
    func setVisitorInfo(imageName:String?, message:String?) {
        
        if let img = imageName, msg = message {
            
            circleView.hidden = true
            iconView.image = UIImage(named: img)
            textLabel.text = msg
        }else{
            
            starCoreAnimation()
            
        }
        
    }
    
    //MARK: - maskIconView 添加动画效果
    
    private  func starCoreAnimation() {
        
        let animation = CABasicAnimation(keyPath: "transform.rotation")
        
        animation.toValue = 2 * M_PI
        animation.duration = 20
        animation.repeatCount = MAXFLOAT
        animation.removedOnCompletion = false
        circleView.layer.addAnimation(animation, forKey: nil)
        
    }
    
    // MARK: -  懒加载参观视图的控件
    
    //iconView
    
    private lazy var iconView: UIImageView = {
        
        let image = UIImage(named: "visitordiscover_feed_image_house")
        
        let imageView = UIImageView(image: image)
        
        return imageView
        
    }()
    
    // circleView
    
    private lazy var circleView: UIImageView = {
        
        let image = UIImage(named: "visitordiscover_feed_image_smallicon")
        
        let imageView = UIImageView(image: image)
        
        return imageView
        
    }()
    
    //textLabel
    
    private lazy var textLabel: UILabel = {
        
        let label = UILabel()
        
        label.textColor = UIColor.darkTextColor()
        label.numberOfLines = 0
        label.font = UIFont.systemFontOfSize(14)
        label.text = "关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜"
        return label
    }()
    //registerButton
    
     lazy var registerButton: UIButton = {
        
        let button = UIButton()
        
        button.setTitle("注册", forState: .Normal)
        button.setTitleColor(UIColor.orangeColor(), forState: .Normal)
        button.titleLabel?.textAlignment = .Center
        button.titleLabel?.font = UIFont.systemFontOfSize(14)
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)
        return button
        
    }()
    //loginButton
    
     lazy var loginBuuton: UIButton = {
        
        let button = UIButton()
        
        button.setTitle("登录", forState: .Normal)
        button.setTitleColor(UIColor.orangeColor(), forState: .Normal)
        button.titleLabel?.textAlignment = .Center
        button.titleLabel?.font = UIFont.systemFontOfSize(14)
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)
        return button
        
    }()
    //maskIconView
    
    private lazy var maskIconView: UIImageView = {
        
        let image = UIImage(named: "visitordiscover_feed_mask_smallicon")
        
        let imageView = UIImageView(image: image)
        
        return imageView
    }()
    
}
